/*
 * bmm150_i2c.c
 *
 *  Created on: Jun 22, 2025
 *      Author: lsy18
 */


#include "bmm150.h"
#include "bmm150_i2c.h"
#include "data_tran/data_tran.h"


#include "i2c.h"
#include "usart.h"

#include "math.h"

#include "../Hardware/Led/led.h"

#define I2C_ADDRESS 0x26

struct bmm150_dev dev = {0};
struct bmm150_mag_data magnet_data = {0};
uint8_t dev_addr;

// 微秒延时函数
void delay_us(uint32_t us)
{
	HAL_Delay((uint32_t)us/1000);
}

/*!
 * @brief This function is for writing the sensor's registers through I2C bus.
 */
int8_t bmm150_user_i2c_reg_write(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr) {

	HAL_I2C_Mem_Write(&hi2c1, I2C_ADDRESS, reg_addr, I2C_MEMADD_SIZE_8BIT, reg_data, length, 1000);
	return 0;
}

/*!
 * @brief This function is for reading the sensor's registers through I2C bus.
 */
int8_t bmm150_user_i2c_reg_read(uint8_t reg_addr, uint8_t *data, uint32_t length, void *intf_ptr) {

	HAL_I2C_Mem_Read(&hi2c1, I2C_ADDRESS, reg_addr, I2C_MEMADD_SIZE_8BIT, data, length, 1000);

    return 0;
}

void bmm150_user_delay_us(uint32_t period_us, void *intf_ptr) {
	//delay_us_init();
//    HAL_Delay(period_us/1000);
	delay_us(period_us);
	//delay_ms_init();
}


int8_t BMM150_SetConfig(struct bmm150_dev *dev) {
  /* Status of api are returned to this variable. */
  int8_t rslt;

  struct bmm150_settings settings;

  /* Set powermode as normal mode */
  settings.pwr_mode = BMM150_POWERMODE_NORMAL;
  rslt = bmm150_set_op_mode(&settings, dev);

  if (rslt == BMM150_OK) {
    /* Setting the preset mode as Low power mode
         * i.e. data rate = 10Hz, XY-rep = 1, Z-rep = 2
         */
    settings.preset_mode = BMM150_PRESETMODE_LOWPOWER;
    rslt = bmm150_set_presetmode(&settings, dev);
//	settings.data_rate = BMM150_DATA_RATE_02HZ;  // TODO Change it to the desired ODR
//    bmm150_set_sensor_settings(BMM150_SEL_DATA_RATE, &settings, dev);

    if (rslt == BMM150_OK) {
      /* Map the data interrupt pin */
      settings.int_settings.drdy_pin_en = 0x01;
      rslt = bmm150_set_sensor_settings(BMM150_SEL_DRDY_PIN_EN, &settings, dev);
    }
  }
  return rslt;
}

int8_t BMM150_Init(struct bmm150_dev *dev) {

	int8_t rslt = BMM150_OK;

	dev->intf = BMM150_I2C_INTF;
	dev_addr = I2C_ADDRESS;
	dev->read = bmm150_user_i2c_reg_read;
	dev->write = bmm150_user_i2c_reg_write;

	/* Assign device address to interface pointer */
	dev->intf_ptr = &dev_addr;
	/* Configure delay in microseconds */
	dev->delay_us = bmm150_user_delay_us;

	rslt = bmm150_init(dev);
    //bmm150_error_codes_print_result("bmm150_init", rslt);

	if (rslt == BMM150_OK) {
		rslt = BMM150_SetConfig(dev);
		//bmm150_error_codes_print_result("set_config", rslt);

		if (rslt == BMM150_OK) {
//			HAL_UART_Transmit(&huart3, 'x', 1, 1000);
			rslt = BMM150_GetData();
			//HAL_UART_Transmit(&huart2, (uint8_t * ) &pos_init_data, 4, 1000);
			//bmm150_error_codes_print_result("get_data", rslt);
		}
	}
	HAL_Delay(50);
	BMM150_GetData();
	pos_init_data = (int16_t)(atan2(magnet_data.y,magnet_data.x) * 180.0 / PI);
	pos_init_data = pos_init_data < 0 ? pos_init_data + 360 : pos_init_data;
	//HAL_UART_Transmit(&huart2, (uint8_t * ) &magnet_data, 6, 1000);
	return rslt;
}


int8_t BMM150_GetData() {
	/* Status of api are returned to this variable. */
	int8_t rslt;
	/* Reading the mag data */

	/* Get the interrupt status */
	rslt = bmm150_get_interrupt_status(&dev);
//	rslt = bmm150_read_mag_data(&magnet_data_data, &dev);
//	rslt = bmm150_read_mag_data(&magnet_data, &dev);
	if (dev.int_status & BMM150_INT_ASSERTED_DRDY) {
	    /* Read mag data */
	    rslt = bmm150_read_mag_data(&magnet_data, &dev);
	    bmm150_tran_jy62(&magnet_data);
	}
  return rslt;
}




